unit UnitSaveSession;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ICore.Utils;

type
  TFormSaveSession = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FFileName: string;
    hEsc: LongInt;
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
  public
    { Public declarations }
    property FileName: string read FFileName;
  end;

var
  FormSaveSession: TFormSaveSession;

implementation

{$R *.dfm}

procedure TFormSaveSession.Button1Click(Sender: TObject);
begin
  Edit1.Text := TUtils.Trim(Edit1.Text);
  Edit1.Text := TUtils.ReplaceChars(Edit1.Text, ['>', '<', '|', '?', '*', '/', '\', ':', '"'], '_');
  if (Edit1.Text <> '') and
      (Edit1.Text <> '.') and
      (Edit1.Text <> '..') then
  begin
    FFileName := Edit1.Text;
    ModalResult := mrOk;
  end;
end;

procedure TFormSaveSession.Button2Click(Sender: TObject);
begin
  FFileName := '';
  ModalResult := mrCancel;
end;

procedure TFormSaveSession.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) then
   Button1Click(Sender);
end;

procedure TFormSaveSession.FormCreate(Sender: TObject);
begin
  ModalResult := mrCancel;
  FFileName := '';
  hEsc := GlobalAddAtom('hEsc_SaveSession');
  RegisterHotKey(Handle, hEsc, 0, VK_ESCAPE);
end;

procedure TFormSaveSession.FormDestroy(Sender: TObject);
begin
  UnregisterHotKey(Handle, hEsc);
end;

procedure TFormSaveSession.FormShow(Sender: TObject);
begin
  FFileName := '';
  Edit1.Text := FFileName;
  Edit1.SetFocus;
end;

procedure TFormSaveSession.WMHotKey(var Msg: TWMHotKey);
begin
  with Msg do
  begin
    if (HotKey = hEsc) then
     ModalResult := mrCancel;
  end;
end;

end.
